<?php
/**
 * @Project: None
 * @FileName: GameTxt.php
 * @FilePath: D:\thinkphp6\tp\app\controller\GameTxt.php
 * @Author: zishuiliufeng
 * @Date:   2021-01-28 17:21:49
 * @Last Modified by:   zishuiliufeng
 * @Last Modified time: 2021-02-13 18:05:20
 */
namespace app\controller;
use think\Facade\Db;
use think\facade\Request;
use app\BaseController;
use app\Response;
use app\model\RoleInfo;
use app\model\RoleEvent;
use app\model\GameInfo;

class GameTxt extends BaseController{

    /**
     * [getList 获取游戏内容列表]
     * @return [json] [description]
     */
    public function getList(){
        $param = $this->request->param();
        $page = isset($param['page'])?(int)$param['page']:1;
        $perpage = isset($param['perpage'])?(int)$param['perpage']:10;

        $role_id = isset($param['role_id'])?(int)$param['role_id']:0;
        //发生的时间段
        $time_slot = isset($param['time_slot'])?(string)$param['time_slot']:'';
        //状态
        $status = isset($param['role_id'])?(int)$param['role_id']:2;

        $where = [];
        if(!empty($time_slot)){
            $where[] = ['time_slot','like','%'.$time_slot.'%'];
        }

        if($status == 1 || $status == 0){
            $where[] = ['status','=',$status];
        }

        $list = Db::name('game_info')->where($where)->order('id' ,'asc')->paginate($perpage)->each(function($item,$key){
            $event = $this->getRoleEventById($item['role_event_id']);
            $item['role_event_name'] = isset($event['name'])?$event['name']:'';
            $item['role_event_en_name'] = isset($event['en_name'])?$event['en_name']:'';
            return $item;
        });
    
        //当前页数
        $currentPage = $list->currentPage();
        // 获取总记录数
        $total = $list->total();
        //最后页数
        $lastPage = $list->lastPage();
        //每页显示数
        $listRows = $list->listRows();
        //数据
        $data = $list->items();
        
        $result = array(
            'currentPage' => $currentPage,
            'total' => $total,
            'lastPage' => $lastPage,
            'listRows' => $listRows,
            'content' => $data
        );
        Response::show(200,'ok',$result);
    }

    public function getRoleEventById($id=0)
    {
        $result = array();
        if($id > 0){
            $event = RoleEvent::where('id',$id)->findOrEmpty();
            if(!$event->isEmpty())
            {
                $result = $event;
            }
        }
        return $result;
    }
    /**
     * [addGameContent 新增游戏文本信息]
     */
    public function addGameContent()
    {
        $param          = $this->request->param();
        $role_id        = isset($param['role_id'])?(int)$param['role_id']:0;
        $game_id        = isset($param['game_id'])?(int)$param['game_id']:0;
        $role_event_id  = isset($param['role_event_id'])?(int)$param['role_event_id']:0;

        $time_slot  = isset($param['time_slot'])?(string)$param['time_slot']:'';
        $img        = isset($param['img'])?(string)$param['img']:'';
        $video      = isset($param['video'])?(string)$param['video']:'';
        $content    = isset($param['content'])?(string)$param['content']:'';
        $path       = isset($param['path'])?(string)$param['path']:'';

        $name       = isset($param['name'])?(string)$param['name']:'';
        $result     = isset($param['result'])?(string)$param['result']:'';

        if($role_id < 1){
            $result = array('role_id'=>$role_id);
            Response::show(201,'参数错误',$result);
        }

        //判断角色是否存在
        $role = RoleInfo::where('id',$role_id)->findOrEmpty();
        if($role->isEmpty()){
            Response::show(404,'角色不存在');
        }
        //如果game_id未传值
        if($game_id < 1){
            $game_id = $role->game_id;
        }

        //判断人物事件是否存在
        if($role_event_id > 0){
            $roleEvent = RoleEvent::where('id',$role_event_id)->findOrEmpty();
            if($roleEvent->isEmpty()){
                Response::show(404,'人物事件不存在');
            }
            //判断事件是否已使用
            $is_used = $roleEvent->is_used;
            if($is_used > 0){
                Response::show(206,'该事件已使用');
            }
        }
        
        //保存游戏文本信息
        $info = new GameInfo;
        $info->game_id          = $game_id;
        $info->role_id          = $role_id;
        $info->role_event_id    = $role_event_id;
        $info->time_slot        = $time_slot;
        $info->img              = $img;
        $info->video            = $video;
        $info->content          = $content;
        $info->path             = $path;
        $info->name             = $name;
        $info->result           = $result;
        $info->status           = 0;
        $info->create_time      = date('Y-m-d H:i:s');
        $info->update_time      = date('Y-m-d H:i:s');
        $info->save();
        //获取自增ID
        $id = $info->id;
        Response::show(200,'新增成功',$id);
    }
    /**
     * [editGameContent 编辑游戏文本]
     * @return [json] [description]
     */
    public function editGameContent()
    {
        $param          = $this->request->param();
        $id             = isset($param['id'])?(int)$param['id']:0;
       
        $role_event_id  = isset($param['role_event_id'])?(int)$param['role_event_id']:0;

        $status  = isset($param['status'])?(int)$param['status']:0;


        $time_slot  = isset($param['time_slot'])?(string)$param['time_slot']:'';
        $img        = isset($param['img'])?(string)$param['img']:'';
        $video      = isset($param['video'])?(string)$param['video']:'';
        $content    = isset($param['content'])?(string)$param['content']:'';
        $path       = isset($param['path'])?(string)$param['path']:'';

        $name       = isset($param['name'])?(string)$param['name']:'';
        $result     = isset($param['result'])?(string)$param['result']:'';

        //判断游戏内容是否存在
        $info = GameInfo::where('id',$id)->findOrEmpty();
        if($info->isEmpty()){
            Response::show(404,'游戏内容不存在');
        }

        //判断人物事件是否存在
        if($role_event_id > 0){
            $roleEvent = RoleEvent::where('id',$role_event_id)->findOrEmpty();
            if($roleEvent->isEmpty()){
                Response::show(404,'人物事件不存在');
            }
            //判断事件是否已使用
            $is_used = $roleEvent->is_used;
            if($is_used > 0){
                Response::show(206,'该事件已使用');
            }
        }
        
        //保存游戏文本信息
        $info->role_event_id    = $role_event_id;
        $info->time_slot        = $time_slot;
        $info->img              = $img;
        $info->video            = $video;
        $info->content          = $content;
        $info->path             = $path;
        $info->status           = $status;
        $info->name             = $name;
        $info->result           = $result;
        $info->update_time      = date('Y-m-d H:i:s');
        $info->save();
        Response::show(200,'编辑成功',$id);
    }
    /**
     * [delGameInfo 删除游戏内容]
     * @return [json] [description]
     */
    public function delGameInfo()
    {
        $param          = $this->request->param();
        $id             = isset($param['id'])?(int)$param['id']:0;

        //判断游戏内容是否存在
        $info = GameInfo::where('id',$id)->findOrEmpty();
        if($info->isEmpty()){
            Response::show(404,'游戏内容不存在');
        }

        //重置人物事件的使用状态
        $role_event_id = $info->role_event_id;
        if(!empty($role_event_id)){
            $event = RoleEvent::where('id',$role_event_id)->findOrEmpty();
            if(!$event->isEmpty()){
                $event->is_used = 0;
                $event->update_time = date('Y-m-d H:i:s');
                $event->save();
            }
        }

        $del = $info->delete();    //bool事件，删除成功返回true,失败返回false
        if($del){
            $code = 200;
            $msg = '删除成功';
        }else{
            $code = 204;
            $msg = '删除失败';
        }

        Response::show($code,$msg);
    }
}